﻿using System;
using System.Collections.Generic;
using System.Text;


using CMN.Common;
using CMN.Entity;
using CMN.BLL;
using CMN.Cache;
using CMN.Extends;
using System.IO;
namespace CMN.Extends.Job
{
    public class JobCollcetUpdateContent : IJob
    {
        public JobCollcetUpdateContent()
        {

        }
        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="obj"></param>
        public void Execute(object obj)
        {
            try
            {
                BLLHelper bll = new BLLHelper();
                QueryParam qp = new QueryParam();
                bool isCollect = false;
                qp.ReturnFields = "*";
                qp.PageIndex = 1;
                qp.PageSize = 10;

                qp.Orderfld = " ";
                int recordNum = 0;
                Extend_CollectLinkInfo bllLinkInfo = new Extend_CollectLinkInfo();
                Extend_CollectSetInfo bllSetInfo = new Extend_CollectSetInfo();
                Extend_CollectContent bllContent = new Extend_CollectContent();
                Module_Article bllArticle = new Module_Article();
                List<Extend_CollectLinkInfoEntity> linkList = null;
                List<Extend_CollectSetInfoEntity> setList = CacheCollcetSet.CollectSetInfoList();
                foreach (Extend_CollectSetInfoEntity setInfo in setList)
                {


                    // AppLog.Write(setInfo.Name + setInfo.CollectContentTime.ToString(), AppLog.LogMessageType.Debug);

                    qp.Where = string.Concat(" CollectSetId=", setInfo.Id.ToString(), " and IsCollect=1 and  CollectNum=9  ");
                    qp.IsReturnCount = false;
                    linkList = bllLinkInfo.List(qp, out recordNum);
                    foreach (Extend_CollectLinkInfoEntity linkInfo in linkList)
                    {
                        isCollect = true;
                        // Extend_CollectLinkInfoEntity linkInfo = linkList[0];

                        CollectHelper chelper = new CollectHelper();
                        Extend_CollectContentEntity content = new Extend_CollectContentEntity();

                        try
                        {
                            try
                            {
                                if (setInfo.IsPaging)
                                {
                                    content = chelper.GetPagingContent(setInfo, linkInfo.Url);

                                }
                                else
                                    content = chelper.GetContent(setInfo, linkInfo.Url);
                            }
                            catch (Exception ex)
                            {
                                AppLog.Write(string.Concat("采集内容错误", ex.Message, setInfo.Name.ToString(), ":", linkInfo.Url), AppLog.LogMessageType.Error, ex);
                                content.TextContent = "";
                            }
                            if (content.TextContent.Length > 20)
                            {
                                content.SourceFrom = linkInfo.Url;
                                content.AddDate = DateTime.Now;
                                content.ChannelId = setInfo.ChannelId;
                                content.CollectSetId = setInfo.Id;
                                content.Enabled = false;
                                content.IsPass = setInfo.IsAutoUpdate;
                                content.DataTable_Action = DataTableAction.Insert;
                                content.Title = content.Title.Replace("'", "").Trim();
                                Module_ArticleEntity    article = bllArticle.DispSouceFrom(linkInfo.Url);
                                if (article == null)
                                    article = bllArticle.Disp(content.Title);
                                if (article != null)
                                {
                                    if (article.SourceFrom == linkInfo.Url)
                                    {
                                        if (content.TextContent.Length > article.TextContent.Length || content.Remark.Length > article.Remark.Length)
                                        {
                                            if (article.Title != content.Title)
                                            {
                                                article.Title = content.Title;
                                                article.IsCreateIndex = true;
                                            }
                                            article.TextContent = content.TextContent;
                                            article.Remark = content.Remark;
                                            article.Editor = content.Editor;
                                            if (string.IsNullOrEmpty(article.Img))
                                                article.Img = content.Img;
                                            article.AddDate = DateTime.Now;
                                            article.DataTable_Action = DataTableAction.Update;
                                            bllArticle.InsertUpdateDelete(article);

                                        }
                                    }
                                    else
                                    {
                                        if (!string.IsNullOrEmpty(article.SourceFrom) && (content.TextContent.Length > article.TextContent.Length || content.Remark.Length > article.Remark.Length))
                                        {
                                            if (article.Title != content.Title)
                                            {
                                                article.Title = content.Title;
                                                article.IsCreateIndex = true;
                                            }

                                            article.TextContent = content.TextContent;
                                            article.Remark = content.Remark;
                                            article.Editor = content.Editor;
                                            if (string.IsNullOrEmpty(article.Img))
                                                article.Img = content.Img;
                                            article.AddDate = DateTime.Now;
                                            article.DataTable_Action = DataTableAction.Update;
                                            bllArticle.InsertUpdateDelete(article);


                                        }
                                    }


                                }
                                else
                                {
                                    article = new Module_ArticleEntity();
                                    article.DataTable_Action = DataTableAction.Insert;
                                    article.Title = content.Title;
                                    article.Tags = content.Tags;
                                    article.Summary = content.Summary;
                                    article.TextContent = content.TextContent;
                                   
                                        article.Img = content.Img;
                                    article.Enabled = true;
                                    article.IsPass = 1;
                                    article.ChannelId = content.Id;
                                    Normal_Channel channel = new Normal_Channel();
                                    Normal_ChannelEntity channelEntity = channel.Disp(content.ChannelId);
                                     if (channelEntity != null)
                                     {
                                         article.Path = channelEntity.Path;
                                     }
                                   
                                    article.SourceFrom = content.SourceFrom;
                                    article.Editor = content.Editor;
                                    if (setInfo != null)
                                        article.Author = setInfo.Name;
                                    article.AddDate = DateTime.Now;
                                    article.Remark = content.Remark;
                                    article.OrderNum = content.OrderNum;
                                    if (content.OrderNum < 10 && content.OrderNum > 0)
                                    {
                                        article.Flag = "b";
                                    }
                                    if (setInfo != null && setInfo.IsRecommend)
                                    {
                                        article.Flag = "b";
                                    }                                   
                                    bllArticle.InsertUpdateDelete(article);
                                }

                                linkInfo.DataTable_Action = DataTableAction.Update;
                                linkInfo.CollectNum = 4;
                                bllLinkInfo.InsertUpdateDelete(linkInfo);
                            }
                            else
                            {
                                linkInfo.CollectNum = 3;
                                AppLog.Write(string.Concat("采集内容为空", setInfo.Name.ToString(), ":", linkInfo.Url), AppLog.LogMessageType.Debug);
                            }

                            //setInfo.CollectContentTime = DateTime.Now;
                        }
                        catch (Exception ex)
                        {
                            AppLog.Write(string.Concat("采集循环更新内容错误", setInfo.Name.ToString(), ":", linkInfo.Url), AppLog.LogMessageType.Error, ex);
                        }
                        System.Threading.Thread.Sleep(60);

                    }


                }

                if (!isCollect)
                    System.Threading.Thread.Sleep(20000);
            }
            catch (Exception ex)
            {
                AppLog.Write(string.Concat("采集循环更新内容错误", ex.Message), AppLog.LogMessageType.Error, ex);
            }
        }
        

        public void Error(Exception e)
        {
            AppLog.Write("采集循环更新内容错误", AppLog.LogMessageType.Error, e);
        }
    }
}
